Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: bash shell for production docker builds #6469

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

eatyourgreens
Copy link
Contributor

@eatyourgreens eatyourgreens commented Nov 14, 2024

This adds a bash shell for production docker builds, and renames the previous shell service to dev-shell.

It also optimises the Dockerfile for faster builds.

Please request review from @zooniverse/frontend team or an individual member of that team.

Linked Issue and/or Talk Post

How to Review

Similar to #6465, but with the changes here you should be able to get CLI shells into both production and dev installs of the monorepo:

docker compose build
docker compose run --rm dev-shell
docker compose run --rm prod-shell
docker compose down

I've been using these to run du -sh * inside a production build, and have a look at how big each directory is.

docker compose run --rm prod-shell
[+] Creating 1/0
 ✔ Network front-end-monorepo_default  Created                                                                                                      0.0s 
/usr/src # du -sh *
790.3M	node_modules
4.0K	package.json
641.7M	packages
824.0K	yarn.lock
/usr/src # du -sh packages/*
8.0K	packages/app-content-pages
327.3M	packages/app-project
302.3M	packages/app-root
48.0K	packages/lib-async-states
6.5M	packages/lib-classifier
660.0K	packages/lib-content
60.0K	packages/lib-grommet-theme
344.0K	packages/lib-panoptes-js
1.7M	packages/lib-react-components
236.0K	packages/lib-subject-viewers
2.6M	packages/lib-user
48.0K	packages/tools-standard
/usr/src # exit
docker compose down

When I run ls node_modules inside the production container, I don't see @storybook, mocha or webpack, so I think the production Yarn install is working, and only installing production dependencies. This might be useful for checking whether that's still true after moving to Yarn 4.

You should also be able to inspect the node_modules-dev and node_modules-prod volumes in Docker Desktop, to see the difference between production and development images.

Checklist

PR Creator - Please cater the checklist to fit the review needed for your code changes.
PR Reviewer - Use the checklist during your review. Each point should be checkmarked or discussed before PR approval.

General

  • Tests are passing locally and on Github
  • Documentation is up to date and changelog has been updated if appropriate
  • You can yarn panic && yarn bootstrap or docker-compose up --build and FEM works as expected
  • FEM works in all major desktop browsers: Firefox, Chrome, Edge, Safari (Use Browserstack account as needed)
  • FEM works in a mobile browser

@coveralls
Copy link

coveralls commented Nov 14, 2024

Coverage Status

coverage: 77.851% (-0.02%) from 77.868%
when pulling 377b655 on eatyourgreens:docker-compose-builds
into c146bdf on zooniverse:master.

@@ -27,7 +27,7 @@ docker compose up -d
# stop the local services when you're finished
docker compose down
# run the tests
docker compose run --rm project test
docker compose run --rm root test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo. The Next.js app service is called root in docker-compose.yml.

Comment on lines 2 to 14
prod-shell:
image: front-end-monorepo_prod:latest
build:
context: ./
args:
- NODE_ENV=production
- PANOPTES_ENV=production
- NEXT_TELEMETRY_DISABLED=1
- APP_ENV=development
command:
- "/bin/sh"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs bin/sh inside the front-end-monorepo_prod container, giving you a bash shell so that you can poke around the file system and debug the production build.

dev-shell does the same thing for front-end-monorepo_dev, which has dev dependencies installed (so you can also run tests from that shell.)

image: front-end-monorepo_dev:latest
volumes:
- node_modules:/usr/src/node_modules
Copy link
Contributor Author

@eatyourgreens eatyourgreens Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this to make it easier to debug the node package installs. Otherwise, you're just looking at the node_modules directory from your local machine. I'm going to avoid changes to existing services here. Just add a new service for the production bash shell, and rename the existing service (and volume.)

@eatyourgreens
Copy link
Contributor Author

Here's the output of docker images on this branch.

front-end-monorepo % docker images
REPOSITORY                                   TAG                   IMAGE ID       CREATED          SIZE
front-end-monorepo_prod                      latest                26d2798ccb75   33 minutes ago   1.43GB
front-end-monorepo_dev                       latest                0c20a73cd0f3   35 minutes ago   2.07GB

@eatyourgreens eatyourgreens force-pushed the docker-compose-builds branch 3 times, most recently from 2640253 to 5321a95 Compare November 15, 2024 12:35
Comment on lines 38 to 40
- node_modules:/usr/src/node_modules
volumes:
node_modules:
Copy link
Contributor Author

@eatyourgreens eatyourgreens Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared volumes can be convenient for local development. You can inspect them in the Docker Desktop app. This one should contain all the installed Node development packages.

Screenshot of the front-end-monorepo_node_modules volume in Docker Desktop, showing a listing of the volume contents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added two node_modules volumes, so you can compare production vs. development installs in Docker Desktop.

@eatyourgreens eatyourgreens force-pushed the docker-compose-builds branch 2 times, most recently from 870783f to 7a72e98 Compare November 18, 2024 18:39
@goplayoutside3 goplayoutside3 self-assigned this Nov 18, 2024
- Add the lockfile last, so that previous layers are cached.
- Copy files from the builder to the production app runner.
@@ -40,6 +38,8 @@ ADD lerna.json /usr/src/

COPY ./packages /usr/src/packages

ADD yarn.lock /usr/src/
Copy link
Contributor Author

@eatyourgreens eatyourgreens Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarn.lock changes quite frequently, so should be added to the image after all other files.

Comment on lines +67 to +76
COPY --from=builder /usr/src/package.json /usr/src/package.json

COPY .yarn /usr/src/.yarn
COPY --from=builder /usr/src/.yarn /usr/src/.yarn

ADD .yarnrc /usr/src/
COPY --from=builder /usr/src/.yarnrc /usr/src/.yarnrc

COPY --from=builder /usr/src/packages ./packages

COPY --from=builder /usr/src/yarn.lock /usr/src/yarn.lock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything that the production runner uses should be copied from the builder image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants